package com.xiguaji.boot.eventbus;


import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

//不同事件的实现类不能相同,不同事件必须有不同的实现类,否则会导致事件被多个EventListener重复执行
public abstract class Event {

    public Event(String name) {
        this.name = name;
    }

    public Event(Event event, String name) {
        this(name);
        this.triggerEvent = event;
    }

    private String name;

    private Event triggerEvent;

    public String getSource() {
        Event event = triggerEvent;
        List<String> names = new ArrayList<>();
        while (event != null) {
            names.add(event.name);
            event = event.triggerEvent;
        }

        if (CollectionUtils.isEmpty(names)) return this.name;

        Collections.reverse(names);
        return names.stream().collect(Collectors.joining("-")) + "-" + this.name;
    }
}
